package screens;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import main.PointSensorPair;
import main.Robot;
import main.Sensor;
import main.UserInterface;

public class SensorSelectionBox extends JPanel implements MouseListener {

	private ArrayList<PointSensorPair> pairs;	
	
	public SensorSelectionBox() {
        setPreferredSize(new Dimension(800, 400));
        this.addMouseListener(this);
        
        pairs = new ArrayList<PointSensorPair>();
        
        pairs.add(new PointSensorPair(Robot.northSonar));
        pairs.add(new PointSensorPair(Robot.eastSonar));
        pairs.add(new PointSensorPair(Robot.southSonar));
        pairs.add(new PointSensorPair(Robot.westSonar));
        
        pairs.add(new PointSensorPair(Robot.northIR));
        pairs.add(new PointSensorPair(Robot.northEastIR));
        pairs.add(new PointSensorPair(Robot.eastIR));
        pairs.add(new PointSensorPair(Robot.southEastIR));
        pairs.add(new PointSensorPair(Robot.southIR));
        pairs.add(new PointSensorPair(Robot.southWestIR));
        pairs.add(new PointSensorPair(Robot.westIR));
        pairs.add(new PointSensorPair(Robot.northWestIR));
                
        repaint();
	}
	
	// Paint component for drawing the environment
    public void paintComponent(Graphics g) {
    	g.clearRect(0, 0, 800, 400);
        g.fillOval(350, 150, 100, 100);
        paintSensors(g);
    }
    
    private void paintSensors(Graphics g) {
    	for (int i=0; i<pairs.size(); i++) {
    		paintSensor(g, pairs.get(i));
    	}
    }
    
    private void paintSensor(Graphics g, PointSensorPair p) {
    	if (p.sensor.active) {
    		g.fillRect((int) (400+p.point.x-5), (int) (200-p.point.y-5), 10, 10);
    	} else {
    		g.drawRect((int) (400+p.point.x-5), (int) (200-p.point.y-5), 10, 10);
    	}
    	
    }
    
	@Override
	public void mouseClicked(MouseEvent e) {		
		Point click = e.getPoint();
		click.x = click.x-400;
		click.y = 200-click.y;
		
		double min_dist = 500;
		double dist;
		Sensor s = null;;
		
		for (int i=0; i<pairs.size(); i++) {
    		dist = Math.sqrt(Math.pow(click.x-pairs.get(i).point.x, 2)+Math.pow(click.y-pairs.get(i).point.y, 2));
    		if (dist < min_dist) {
    			min_dist = dist;
    			s = pairs.get(i).sensor;
    		}
    	}
		
		if (min_dist < 20) {
			s.active = !(s.active);
		}
		
		repaint();
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}